home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CHARTP10.ARJ / CATEGORY.CPP < prev    next >
C/C++ Source or Header  |  1992-01-26  |  3KB  |  157 lines

  1.  
  2. // Copyright 1992, David Perelman-Hall & Jamshid Afshar
  3.  
  4. #include <assert.h>
  5. #include "category.h"
  6.  
  7.  
  8. ostream& operator << ( ostream& os, const Category& cat )
  9. {
  10.    os << cat._s;
  11.    return os;
  12. }
  13.  
  14. //destructor
  15. Category::~Category()
  16. {
  17.    if (alive == DEAD)
  18.       abort();
  19.    if (alive != ALIVE)
  20.       abort();
  21.    alive = DEAD;
  22. }
  23.  
  24.  
  25. Category_Sequence::Category_Sequence()
  26.    : _first_node(NULL) { alive = ALIVE; }
  27.  
  28. Category_Sequence::Category_Sequence( const Category_Sequence& seq)
  29.    : _first_node(NULL)
  30. {
  31.    alive = ALIVE;
  32.    *this = seq;
  33. }
  34.  
  35. Category_Sequence::Category_Sequence( const Category& category )
  36.    : _first_node(NULL) 
  37.    alive = ALIVE; 
  38.    *this += category;
  39. }
  40.  
  41. Category_Sequence::~Category_Sequence()
  42. {
  43.    assert(alive==ALIVE);
  44.    alive = DEAD;
  45.    clear();
  46. }
  47.  
  48. Category Category_Sequence::pop()
  49. {
  50.    assert(!isEmpty());
  51.    Category cat = _first_node->_category;
  52.    CategoryNode *temp = _first_node;
  53.    _first_node = _first_node->_nextp;
  54.    delete temp;
  55.    return cat;
  56. }
  57.  
  58. //operator =
  59. void Category_Sequence::operator = ( const Category_Sequence& seq)
  60. {
  61.    clear();
  62.  
  63.    const CategoryNode *catnp = seq._first_node;
  64.    CategoryNode **thiscatnpp = &_first_node;
  65.    while ( catnp != NULL ){
  66.       *thiscatnpp = new CategoryNode(catnp->_category, NULL);
  67.       thiscatnpp = &((*thiscatnpp)->_nextp);
  68.       catnp = catnp->_nextp;
  69.    }
  70. }
  71.  
  72. //operator +=
  73. Category_Sequence& Category_Sequence::operator += ( const Category& category)
  74. {
  75.    assert(strcmp(category, "") != 0);
  76.    CategoryNode **thiscatnpp = &_first_node;
  77.    while( *thiscatnpp != 0 )
  78.       thiscatnpp = &((*thiscatnpp)->_nextp);
  79.    *thiscatnpp = new CategoryNode(category, NULL);
  80.    return *this;
  81. }
  82.  
  83. //operator +=
  84. Category_Sequence& Category_Sequence::operator += ( const Category_Sequence& seq)
  85. {
  86.    const CategoryNode *catnp = seq._first_node;
  87.    while ( catnp != NULL ){
  88.       *this += catnp->_category;
  89.       catnp = catnp->_nextp;
  90.    }
  91.    return *this;
  92. }
  93.  
  94. void Category_Sequence::clear()
  95. {
  96.    while ( !isEmpty() )
  97.       pop();
  98. }
  99.  
  100. Category_Sequence Category_Sequence::rest() const
  101. {
  102.    Category_Sequence seq = *this;
  103.    seq.pop();
  104.    return seq;
  105. }
  106.  
  107. int Category_Sequence::length() const
  108. {
  109.    int n = 0;
  110.    const CategoryNode *catnp = _first_node;
  111.    while ( catnp != NULL ){
  112.       n += 1;
  113.       catnp = catnp->_nextp;
  114.    }
  115.    return n;
  116. }
  117.  
  118. bool Category_Sequence::operator == ( const Category_Sequence& seq) const
  119. {
  120.    CategoryNode *s1 = _first_node;
  121.    CategoryNode *s2 = seq._first_node;
  122.    while ( s1 != NULL && s2 != NULL ) {
  123.       if ( s1->_category != s2->_category )
  124.          return FALSE;
  125.       s1 = s1->_nextp;
  126.       s2 = s2->_nextp;
  127.       }
  128.    return s1==NULL && s2==NULL;
  129. }
  130.  
  131. ostream& operator << ( ostream& os, const Category_Sequence& seq )
  132. {
  133.    const CategoryNode *catnp = seq._first_node;
  134.    bool is_first = TRUE;
  135.    while ( catnp != NULL ){
  136.       if (!is_first)
  137.          os << " ";
  138.       os << catnp->_category;
  139.       catnp = catnp->_nextp;
  140.       is_first = FALSE;
  141.    }
  142.    return os;
  143. }
  144.  
  145. istream& operator >> ( istream& is, Category_Sequence& seq )
  146. {
  147.    seq.clear();
  148.    char cat_str[MAX_STRING_LEN];
  149.    while (is.good()) {
  150.       is >> cat_str;
  151.       if (is.fail()) break;
  152.       seq += Category(cat_str);
  153.    }
  154.    return is;
  155. }
  156.